// --------------------------------------------------------------------------------------------------------------------
// <copyright file="IRCameraModelAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Vision.Visualization
{
    using System;
    using System.Runtime.Serialization;
    using System.Threading.Tasks;

    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Vision.Cameras;
    using Microsoft.Robotics.Vision.Runtime.Cameras;
    using Microsoft.Robotics.Visualization;
    
    /// <summary>
    /// This model agent class prepares the color frame for visualization.
    /// </summary>
    /// <typeparam name="TIRParameters">The type of $IR$ parameters</typeparam>
    [DataContract]
    public sealed class IRCameraModelAgent<TIRParameters> : ModelAgent<IRCameraAgentMessage<TIRParameters>, IRCameraModel>
        where TIRParameters : IRParameters
    {
        /// <summary>
        /// Initializes a new instance of the IRCameraModelAgent class
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="producer">The name of the agent we subscribe to.</param>
        /// <param name="maxUpdateIntervalMilliseconds">The interval on which to issue data binding updates to the UI</param>
        public IRCameraModelAgent(
            string name,
            IProducer<IRCameraAgentMessage<TIRParameters>> producer,
            int maxUpdateIntervalMilliseconds)
            : base(name, producer.Name, maxUpdateIntervalMilliseconds)
        {
        }

        /// <summary>
        /// Initializes any fields not provided in the manifest.
        /// </summary>
        /// <param name="locator">Not used.</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);
        }

        /// <summary>
        /// Updates the model data with the new state.
        /// </summary>
        /// <param name="state">The new state from the upstream agent.</param>
        /// <returns>true if the model was updated, false otherwise</returns>
        protected override bool UpdateModel(IRCameraAgentMessage<TIRParameters> state)
        {
            byte[] image = new byte[state.ImageFrame.Width * state.ImageFrame.Height * 2];

            Parallel.For(
                0,
                state.ImageFrame.ImageData.Length,
                (i) =>
                {
                    unsafe
                    {
                        fixed (ushort* ptr = &(state.ImageFrame.ImageData[i]))
                        {
                            int index = i * 2;
                            image[index] = (byte)(*ptr);
                            image[index + 1] = *((byte*)ptr + 1);
                        }
                    }
                });

            this.Model.VideoFrame = new VideoFrame()
            {
                Data = image,
                Size = new System.Drawing.Size(state.ImageFrame.Width, state.ImageFrame.Height),
                Format = SimplePixelFormat.Gray16,
                Stride = state.ImageFrame.Width * 2
            };

            return true;
        }
    }
}
